﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Shoozla.ViewModel;
using Shoozla.View;
using System.Threading;
using Shoozla.DataModel;

namespace Shoozla
{
    /// <summary>
    /// Interaction logic for FolderListView.xaml
    /// </summary>
    public partial class FolderListView : UserControl
    {

        public FolderListView()
        {
            InitializeComponent();
            FolderListViewModel folderList = new FolderListViewModel(System.Windows.Threading.Dispatcher.CurrentDispatcher);
            DataContext = folderList;
            folderList.ItemRead += new EventHandler(folderList_FileRead);
            folderList.ItemOperationStarted += new ViewModelBase.ItemOperationStartedHandler(folderList_FileOperationStarted);
            folderList.CoverFound += new EventHandler(folderList_CoverFound);
        }

        public void HandleDoubleClick(Object sender, MouseButtonEventArgs e)
        {
            var song = ((ListViewItem)sender).Content as Mp3File;
            ((FolderListViewModel)DataContext).EditMp3(song);
        }

        void folderList_CoverFound(object sender, EventArgs e)
        {
            App.Logger.CoversFound++;
        }

        void folderList_FileOperationStarted(int num, string action)
        {
            Thread newWindowThread = new Thread(new ParameterizedThreadStart(StartProgressWindowInternal));
            newWindowThread.SetApartmentState(ApartmentState.STA);
            newWindowThread.IsBackground = true;
            newWindowThread.Start(new object[] { num, action });
        }

        private void StartProgressWindowInternal(object o)
        {
            object[] param = o as object[];
            //App.ProgressBarWindow = new ProgressBarView((int)param[0], (string)param[1]);
            //App.ProgressBarWindow.ShowDialog();
            App.MainView.InitProgressBar((int)param[0], (string)param[1]);
            System.Windows.Threading.Dispatcher.Run();
        }

        void folderList_FileRead(object sender, EventArgs e)
        {
            //if (App.ProgressBarWindow != null)
            //{
                if (App.CurrenAppStatus == App.AppStatus.SearchingCovers)
                    App.Logger.FilesProcessed++;

                //App.ProgressBarWindow.IncrementBar();
            //}

                App.MainView.IncrementBar();
        }
    }
}
